import csv
import json

from openpyxl import load_workbook


from setting import DATA_DIR
from os import path

class Reader:
    @classmethod
    def read_excel(cls,xlname, min_row, max_row, min_col, max_col):
        xlname = path.join(DATA_DIR,xlname)
        data = []
        wb = load_workbook(xlname)
        ws = wb.active

        # 将选中区域转换为列表
        ls = list(ws.iter_rows(min_row=min_row, max_row=max_row, min_col=min_col, max_col=max_col))
        wb.save(xlname)

        for row in ls:
            data_row = []
            for col in row:
                data_row.append(col.value)
            data.append(data_row)

        wb.save(xlname)
        return data

    @classmethod
    def read_csv(cls, filename):
        file_name = path.join(DATA_DIR,filename)
        ls  = None
        with open(file=file_name,mode='r',encoding='utf-8') as f:
            ls = list(csv.reader(f))
        return ls

if __name__ == '__main__':

    # res1 = Reader.read_excel('wx.xlsx',min_row=28,max_row=32,min_col=9,max_col=12)
    # res2 = Reader.resd_excel('wx.xlsx', min_row=9, max_row=15, min_col=9, max_col=12)
    res3 = Reader.read_csv('search_log_by_username.csv')
    for i in res3:
        print(i)